package com.ruoyi.device.service.impl;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.device.domain.SysDeviceType;
import com.ruoyi.device.mapper.SysDeviceTypeMapper;
import com.ruoyi.device.service.ISysDeviceTypeService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class SysDeviceTypeServiceImpl implements ISysDeviceTypeService {

    @Autowired
    private SysDeviceTypeMapper deviceTypeMapper;
    @Autowired
    private ISysUserService sysUserService;

    @Override
    public SysDeviceType selectSysDeviceTypeById(Long typeId) {
        SysDeviceType sysDeviceType = deviceTypeMapper.selectDeviceTypeById(typeId);
        //创建人名称
        SysUser createUser = sysUserService.selectUserById(Long.parseLong(sysDeviceType.getCreateBy()));
        sysDeviceType.setCreateBy(createUser.getNickName());
        //修改人名称
        SysUser updateUser = sysUserService.selectUserById(Long.parseLong(sysDeviceType.getUpdateBy()));
        sysDeviceType.setUpdateBy(updateUser.getNickName());
        return sysDeviceType;
    }

    @Override
    public List<SysDeviceType> selectDeviceTypeList(SysDeviceType deviceType) {
        List<SysDeviceType> sysDeviceTypeList = deviceTypeMapper.selectDeviceTypeList(deviceType);
        sysDeviceTypeList.stream().map(sysDeviceType -> {
            sysDeviceType.setCreateBy(sysUserService.selectUserById(Long.parseLong(sysDeviceType.getCreateBy())).getNickName());
            sysDeviceType.setUpdateBy(sysUserService.selectUserById(Long.parseLong(sysDeviceType.getUpdateBy())).getNickName());
            return sysDeviceType;
        }).collect(Collectors.toList());
        return sysDeviceTypeList;
    }

    @Override
    public int insertDeviceType(SysDeviceType deviceType) {
        deviceType.setCreateBy(SecurityUtils.getUserId().toString());
        deviceType.setUpdateBy(SecurityUtils.getUserId().toString());
        return deviceTypeMapper.insertDeviceType(deviceType);
    }

    @Override
    public int updateDeviceType(SysDeviceType deviceType) {
        deviceType.setUpdateBy(SecurityUtils.getUserId().toString());
        return deviceTypeMapper.updateDeviceType(deviceType);
    }

    @Override
    public int deleteDeviceTypeByIds(Long[] typeIds) {
        return deviceTypeMapper.deleteDeviceTypeByIds(typeIds);
    }
}
